<script setup lang="ts">
import {onMounted, ref} from "vue"
import AddEditFormVue from "./ApsOrderGoodsBomKittingTemplateAddEditForm.vue"
import TableBar from "@/layouts/components/TableBar/index.vue"
import {ElTable} from "element-plus";
import {HeaderInfo, postResultInfo} from "@@/utils/common-js.ts"
import {type ApsOrderGoodsBomKittingTemplate} from "./ApsOrderGoodsBomKittingTemplateType.ts"
import {Factory, queryFactoryList} from "@v/base/Factory/FactoryType.ts";

const dtoUrl = ref<string>("/apsOrderGoodsBomKittingTemplate")
const documentTitle = ref<string>("齐套模板")
const dataBatchDeleteUrl = ref<string>(`${dtoUrl.value}/deleteByIdList`)

// 查询表格
const queryForm = ref<ApsOrderGoodsBomKittingTemplate>({
  kittingTemplateNo: undefined,
  kittingTemplateName: undefined,
  kittingTemplateUserConfigList: undefined,
  kittingTemplateSaleConfigList: undefined,
  kittingTemplateOrderConfigList: undefined,
  kittingTemplateOrderUserConfigList: undefined,
  factoryId: undefined,
  id: undefined
})

// 表格
// 表格选中的id
const multipleSelection = ref<(string | undefined)[]>([])
// const dataTableRef = ref<InstanceType<typeof ElTable> | null>(null)
const dataTableRef = ref({})
// 表格操作头
const tableBarRef = ref<InstanceType<typeof TableBar> | null>(null)
// 表格相关
const dataList = ref<ApsOrderGoodsBomKittingTemplate[]>([])
const currentPageNum = ref<number>(1)
const currentPageSize = ref<number>(10)
const tableTotal = ref<number>(0)
const factoryList = ref<Factory[]>([])

const headerList = ref<HeaderInfo[]>([
  {fieldName: "id", showName: "序号"},
  {fieldName: "kittingTemplateNo", showName: "模板编号"},
  {fieldName: "kittingTemplateName", showName: "模板名称"},
  {fieldName: "kittingTemplateUserConfigList", showName: "用户配置"},
  {fieldName: "kittingTemplateSaleConfigList", showName: "销售配置"},
  {fieldName: "kittingTemplateOrderConfigList", showName: "订单配置"},
  {fieldName: "kittingTemplateOrderUserConfigList", showName: "订单配置"},
  {fieldName: "factoryId", showName: "工厂ID"},
])

// 获取表格内数据
const getDataList = () => {
  const req = {
    pageSize: currentPageSize.value,
    pageNum: currentPageNum.value,
    data: queryForm.value
  }
  console.info("getDataList {}", req)
  postResultInfo(`${dtoUrl.value}/queryPageList`, req)
  .then((t) => {
    dataList.value = t.data.dataList
    tableTotal.value = Number.parseInt(t.data.total)
    headerList.value = t.data.headerList
  })
}

// table点击事件
const editData = (data: any) => {
  // console.info("data ", data)
  tableBarRef.value?.showEditDialog(data.id)
}
// 页面条数变更事件
const handleSizeChange = (val: number) => {
  currentPageSize.value = val
  getDataList()
}
// 页面变更事件
const handleCurrentChange = (val: number) => {
  currentPageNum.value = val
  getDataList()
}
// 表格选中事件
const handleSelectionChange = (val: ApsOrderGoodsBomKittingTemplate[]) => {
  multipleSelection.value = val.map(t => t.id)
  console.info("multipleSelection ", multipleSelection)
}


// 页面加载事件
onMounted(() => {
  getDataList()
  queryFactoryList().then(r => factoryList.value = r)
})
</script>

<template>
  <div class="app-container">
    <el-card class="search-wrapper" shadow="never">
      <el-form v-model="queryForm" inline>
        <el-form-item label="工厂" prop="factoryId" style="width:200px">
          <el-select v-model="queryForm.factoryId" clearable placeholder="请输入工厂">
            <el-option v-for="f in factoryList" :label="f.factoryName" :key="f.id" :value="f.id"/>
          </el-select>
        </el-form-item>
        <el-form-item label="模板编号" prop="kittingTemplateNo">
          <el-input v-model="queryForm.kittingTemplateNo" clearable placeholder="请输入模板编号"/>
        </el-form-item>
        <el-form-item label="模板名称" prop="kittingTemplateName">
          <el-input v-model="queryForm.kittingTemplateName" clearable placeholder="请输入模板名称"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="getDataList">
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card shadow="never">
      <TableBar
        :document-title="documentTitle"
        :add-component="AddEditFormVue"
        :refresh-list="getDataList"
        :data-table-ref="dataTableRef"
        :multiple-selection="multipleSelection"
        ref="tableBarRef"
        :data-batch-delete-url="dataBatchDeleteUrl"
        :dialog-with="800"
      />
      <ElTable ref="dataTableRef" :data="dataList" stripe @selection-change="handleSelectionChange">
        <ElTableColumn type="selection"/>
        <ElTableColumn prop="kittingTemplateNo" label="编号"/>
        <ElTableColumn prop="kittingTemplateName" label="名称"/>
        <ElTableColumn prop="factoryName" label="工厂"/>
        <ElTableColumn prop="kittingTemplateSaleConfigList" label="工厂">
          <template #default ="scope">
            {{ scope.row.kittingTemplateSaleConfigList.map(t => t.label).join(", ")}}
          </template>
        </ElTableColumn>
        <ElTableColumn prop="kittingTemplateOrderConfigList" label="订单配置">
          <template #default ="scope">
            {{ scope.row.kittingTemplateOrderConfigList.map(t => t.label).join(", ")}}
          </template>
        </ElTableColumn>
        <ElTableColumn prop="kittingTemplateOrderUserConfigList" label="订单用户配置">
          <template #default ="scope">
            {{ scope.row.kittingTemplateOrderUserConfigList.map(t => t.label).join(", ")}}
          </template>
        </ElTableColumn>

        <ElTableColumn fixed="right" label="操作" width="150px">
          <template #default="scope">
            <el-button
              type="warning"
              icon="edit"
              @click="editData(scope.row)"
            >
              编辑
            </el-button>
          </template>
        </ElTableColumn>
      </ElTable>
      <el-row class="paginationDiv">
        <el-pagination
          background
          v-model:current-page="currentPageNum"
          v-model:page-size="currentPageSize"
          layout="total, sizes, prev, pager, next"
          :total="tableTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
  </div>
</template>

<style scoped lang="scss">

</style>

